<?php declare(strict_types=1);

namespace App\Http\Controller;

use App\Lib\RpcClient\RpcClient;
use Swoft\Bean\Annotation\Mapping\Inject;
use Swoft\Consul\Exception\ClientException;
use Swoft\Consul\Exception\ServerException;
use Swoft\Http\Message\Request;
use Swoft\Http\Message\Response;
use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Message\ContentType;
use App\Lib\RpcClient\RpcResult;
use function context;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use Swoft\Http\Server\Annotation\Mapping\Middleware;

/**
 * Class PermissionController
 * @since 2.0
 * @Middleware(name="TenantMiddleware")
 * @Controller()
 */
class PermissionController
{
    /**
     * @Inject()
     *
     * @var RpcClient
     */
    private $RpcClient;

    /**
     * @RequestMapping(route="/{tenant}/allRoles", method={RequestMethod::GET})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function allRoles(Request $request): Response
    {
        $tenant = $request->getAttribute('tenant');
        $data = $request->input();


        /** @var  RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.allRoles')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }

        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/roleCreate", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function roleCreate(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;
        //组装提交参数
        $data['name'] = $request->input('name', '测试角色');
        $data['permission_value'] = $request->input('permission_value', 'role_test');
        $data['frozen_state'] = 0;
        //添加uuid
        $data['uuid'] = uuid();

        /** @var  RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.roleCreate')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }

        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/roleUpdate", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function roleUpdate(Request $request): Response
    {
        $data = $request->input();
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        /** @var  RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.roleUpdate')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }

        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }


    /**
     * @RequestMapping(route="/{tenant}/roleDelete", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function roleDelete(Request $request): Response
    {
        $data = $request->input();
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        /** @var  $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.roleDelete')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }

        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/rolePermissions", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function rolePermissions(Request $request): Response
    {
        $data = $request->input();
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.rolePermissions')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/permissionCreate", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function permissionCreate(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['name'] = $request->input('name', '测试权限');
        $data['permission_value'] = $request->input('permission_value', 'role_test');
        $data['type'] = $request->input('type', 'menu');
        $data['parent_item_id'] = $request->input('parent_item_id', '0');
        $data['remark'] = $request->input('remark', '');
        $data['frozen_state'] = 0;
        //添加uuid
        $data['uuid'] = uuid();

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.permissionCreate')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/roleModifyPermissions", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function roleModifyPermissions(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['role_item_id'] = $request->input('role_item_id', '');
        $data['attached_permission_item_ids'] = $request->input('attached_permission_item_ids', '');
        $data['detached_permission_item_ids'] = $request->input('detached_permission_item_ids', '');

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.roleModifyPermissions')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/userCreate", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function userCreate(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['account'] = $request->input('account', '');
        $data['password'] = $request->input('password', '');
        //添加uuid
        $data['uuid'] = uuid();

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.userCreate')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/positionCreate", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function positionCreate(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['name'] = $request->input('name', '');
        $data['permission_value'] = $request->input('permission_value', '');
        //添加uuid
        $data['uuid'] = uuid();
        $data['frozen_state'] = 0;

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.positionCreate')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/positionModifyUsers", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function positionModifyUsers(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['position_item_id'] = $request->input('position_item_id', '');
        $data['attached_account_item_ids'] = $request->input('attached_account_item_ids', []);
        $data['detached_account_item_ids'] = $request->input('detached_account_item_ids', []);
        //添加uuid

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.positionModifyUsers')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/userPositions", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function userPositions(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['user_item_id'] = $request->input('user_item_id', '');
        //添加uuid

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.userPositions')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/organizationCreate", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function organizationCreate(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['name'] = $request->input('name', '');
        $data['permission_value'] = $request->input('permission_value', '');
        //添加uuid
        $data['uuid'] = uuid();
        $data['frozen_state'] = 0;

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.organizationCreate')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/organizationUpdate", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function organizationUpdate(Request $request): Response
    {
        $data = $request->input();
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        /** @var  RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.organizationUpdate')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }

        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/organizationDelete", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function organizationDelete(Request $request): Response
    {
        $data = $request->input();
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        /** @var  $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.organizationDelete')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }

        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/organizationModifyUsers", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function organizationModifyUsers(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['organization_item_id'] = $request->input('organization_item_id', '');
        $data['attached_account_item_ids'] = $request->input('attached_account_item_ids', []);
        $data['detached_account_item_ids'] = $request->input('detached_account_item_ids', []);
        //添加uuid

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.organizationModifyUsers')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/userOrganizations", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function userOrganizations(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['user_item_id'] = $request->input('user_item_id', '');
        //添加uuid

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.userOrganizations')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/roleModifyUsers", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function roleModifyUsers(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['role_item_id'] = $request->input('role_item_id', '');
        $data['attached_account_item_ids'] = $request->input('attached_account_item_ids', []);
        $data['detached_account_item_ids'] = $request->input('detached_account_item_ids', []);
        //添加uuid

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.roleModifyUsers')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/roleUsers", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function roleUsers(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['role_item_id'] = $request->input('role_item_id', '');
        //添加uuid

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.roleUsers')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/userRolePermissions", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function userRolePermissions(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['user_item_id'] = $request->input('user_item_id', '');

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.userRolePermissions')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/isUserHasPermission", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function isUserHasPermission(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['user_item_id'] = $request->input('user_item_id', '');
        $data['permission_value'] = $request->input('permission_value', '');

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.isUserHasPermission')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

    /**
     * @RequestMapping(route="/{tenant}/listSubLevelPermissions", method={RequestMethod::POST})
     * @param Request $request
     * @return Response
     * @throws ClientException
     * @throws ServerException
     */
    public function listSubLevelPermissions(Request $request): Response
    {
        $data = [];
        $tenant = $request->getAttribute('tenant');
        $data['tenant'] = $tenant;

        //组装提交参数
        $data['user_item_id'] = $request->input('user_item_id', '');
        $data['permission_value'] = $request->input('permission_value', '');

        /** @var RpcResult $RpcResult */
        $RpcResult = $this->RpcClient->getServiceByServiceInfo('Common.Permission.Permission.listSubLevelPermissions')
            ->setParams($data)
            ->query();

        if($RpcResult && $RpcResult->getData()){
            $result = $RpcResult->getData();
        }else{
            $result = serviceReturn(500, '服务错误');
        }
        return context()->getResponse()->withContentType(ContentType::JSON)->withData($result);
    }

}
